﻿using System;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public class ProjectAutoRegisterTool
{
    private static string ProjectDllPath = Application.dataPath + "/" + "Plugins/ProjectGApp/Project.GApp.dll";
    private static string TableStr = "    ";

    [MenuItem("[FC Project]/AutoRegister/自动注册模块", false, 0)]
    private static void AutoRegisterModule()
    {
        AutoRegisterModuleMgr();
        AutoCreateModelConst();
        AutoCreateUIConst();
    }

    [MenuItem("[FC Project]/AutoRegister/自动注册配置表", false, 1)]
    private static void AutoRegisterConfig()
    {
        Assembly assembly = GetProjectAssembly();

        Type baseVO = assembly.GetType("FutureCore.BaseVO");
        string ModelStr = string.Empty;
        foreach (var item in assembly.GetTypes())
        {
            if (item.IsSubclassOf(baseVO))
            {
                DebugUtil.Log("Register:" + item.Name);
                string row = TableStr + TableStr + TableStr + "Add(" + item.Name + "Model.Instance);\n";
                ModelStr += row;
            }
        }

        string originScriptPath = "Assets/_OProject/EditorProject/Editor/Tools/AutoProjectTool/RegisterTool/ConfigMgrRegister.txt";
        string classStr = File.ReadAllText(originScriptPath);
        string replaceConfig = "//ReplaceConfig";
        classStr = classStr.Replace(replaceConfig, ModelStr);

        string targetPath = Application.dataPath.Replace("Client/Assets", string.Empty);
        string subPath = @"Code/Project.GApp/Project.GApp/Scripts/FutureCore/Manager/AutoCreation/ConfigMgrRegister.cs";
        subPath = subPath.Replace('\\', '/');
        targetPath += subPath;
        File.WriteAllText(targetPath, classStr);
        DebugUtil.Log("自动注册配置表完成:" + targetPath);
    }

    private static void AutoRegisterModuleMgr()
    {
        Assembly assembly = GetProjectAssembly();

        Type baseUICtrl = assembly.GetType("FutureCore.BaseUICtrl");
        String ctrlStr = string.Empty;
        foreach (var item in assembly.GetTypes())
        {
            if (item.IsSubclassOf(baseUICtrl))
            {
                string row = TableStr + TableStr + TableStr + "AddUICtrl(new " + item.Name + "());\n";
                ctrlStr += row;
            }
        }

        Type baseModel = assembly.GetType("FutureCore.BaseModel");
        string modelStr = string.Empty;
        foreach (var item in assembly.GetTypes())
        {
            if (item.IsSubclassOf(baseModel))
            {
                DebugUtil.Log("Register:" + item.Name);
                string row = TableStr + TableStr + TableStr + "AddModel(new " + item.Name + "());\n";
                modelStr += row;
            }
        }

        string originScriptPath = "Assets/_OProject/EditorProject/Editor/Tools/AutoProjectTool/RegisterTool/ModuleMgrRegister.txt";
        string classStr = File.ReadAllText(originScriptPath);
        string replaceCtrl = "//ReplaceCtrl";
        string replaceModel = "//ReplaceModel";
        classStr = classStr.Replace(replaceCtrl, ctrlStr);
        classStr = classStr.Replace(replaceModel, modelStr);

        string targetPath = Application.dataPath.Replace("Client/Assets", string.Empty);
        string subPath = @"Code/Project.GApp/Project.GApp/Scripts/FutureCore/Manager/AutoCreation/ModuleMgrRegister.cs";
        subPath = subPath.Replace('\\', '/');
        targetPath += subPath;
        File.WriteAllText(targetPath, classStr);
        DebugUtil.Log("自动注册模块完成:" + targetPath);
    }

    private static void AutoCreateUIConst()
    {
        Assembly assembly = GetProjectAssembly();

        Type baseModel = assembly.GetType("FutureCore.BaseUI");
        string modelConstStr = string.Empty;
        string modelConstClassStr = string.Empty +
            "public static class UIConst\n" +
            "{\n" +
            "//ReplaceModelConst\n" +
            "}";
        foreach (var item in assembly.GetTypes())
        {
            if (item.IsSubclassOf(baseModel))
            {
                string row = TableStr + "public const string " + item.Name + " = \"" + item.Name + "\";\n";
                modelConstStr += row;
            }
        }
        string replaceModelConst = "//ReplaceModelConst";
        modelConstClassStr = modelConstClassStr.Replace(replaceModelConst, modelConstStr);

        string targetPath = Application.dataPath.Replace("Client/Assets", string.Empty);
        string subPath = @"Code/Project.GApp/Project.GApp/Scripts/Application/Define/Const/AutoCreation/UIConstCreate.cs";
        subPath = subPath.Replace('\\', '/');
        targetPath += subPath;
        File.WriteAllText(targetPath, modelConstClassStr);
        DebugUtil.Log("自动创建UI常量完成:" + targetPath);
    }

    private static void AutoCreateModelConst()
    {
        Assembly assembly = GetProjectAssembly();

        Type baseModel = assembly.GetType("FutureCore.BaseModel");
        string modelConstStr = string.Empty;
        string modelConstClassStr = string.Empty +
            "public static class ModelConst\n" +
            "{\n" +
            "//ReplaceModelConst\n" +
            "}";
        foreach (var item in assembly.GetTypes())
        {
            if (item.IsSubclassOf(baseModel))
            {
                string row = TableStr + "public const string " + item.Name + " = \"" + item.Name + "\";\n";
                modelConstStr += row;
            }
        }
        string replaceModelConst = "//ReplaceModelConst";
        modelConstClassStr = modelConstClassStr.Replace(replaceModelConst, modelConstStr);

        string targetPath = Application.dataPath.Replace("Client/Assets", string.Empty);
        string subPath = @"Code/Project.GApp/Project.GApp/Scripts/Application/Define/Const/AutoCreation/ModelConstCreate.cs";
        subPath = subPath.Replace('\\', '/');
        targetPath += subPath;
        File.WriteAllText(targetPath, modelConstClassStr);
        DebugUtil.Log("自动创建模型常量完成:" + targetPath);
    }

    private static Assembly GetProjectAssembly()
    {
        byte[] dllBytes = File.ReadAllBytes(ProjectDllPath);
        Assembly assembly = Assembly.Load(dllBytes);
        return assembly;
    }
}